package api

import (
	"fmt"
	"server/global"
	"server/model/request"
	"server/model/response"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"go.uber.org/zap"
)

type BaseApi struct {
}

var store = base64Captcha.DefaultMemStore

// Captcha 获取图片验证码
// @Tags      公共接口
// @Summary   获取图片验证码
// @Description 获取登录图片验证码
// @accept    application/json
// @Produce   application/json
// @Success   200   {object}  response.Response{data=response.Captcha,msg=string}  "创建成功"
// @Failure   400   {object}  response.Response  "请求参数错误"
// @Failure   500   {object}  response.Response  "内部服务器错误"
// @Router    /base/captcha [post]
func (b *BaseApi) Captcha(c *gin.Context) {
	driver := base64Captcha.NewDriverDigit(
		global.Config.Captcha.Height,
		global.Config.Captcha.Width,
		global.Config.Captcha.Length,
		global.Config.Captcha.MaxSkew,
		global.Config.Captcha.DotCount)

	captcha := base64Captcha.NewCaptcha(driver, store)

	id, b64s, _, err := captcha.Generate()
	if err != nil {
		global.Log.Error("Failed to generate captcha:", zap.Error(err))
		response.FailWithMessage("Failed to generate captcha", c)
		return
	}
	fmt.Printf("id: %s,captcha:%s\n", id, store.Get(id, false))
	response.OkWithData(response.Captcha{
		CaptchaID: id,
		PicPath:   b64s,
	}, c)
}

// SendEmailVerificationCode 发送邮箱验证码
// @Tags      公共接口
// @Summary   发送邮箱验证码
// @Description 发送登录邮箱验证码
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.SendEmailVerificationCode  true  "邮箱和图形验证码"
// @Success   200   {object}  response.Response{msg=string}  "发送成功"
// @Failure   400   {object}  response.Response  "请求参数错误"
// @Failure   500   {object}  response.Response  "内部服务器错误"
// @Router    /base/sendEmailVerificationCode [post]
func (b *BaseApi) SendEmailVerificationCode(c *gin.Context) {
	var req request.SendEmailVerificationCode
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 验证图片验证码
	if store.Verify(req.CaptchaID, req.Captcha, true) {
		// 发送验证码到邮箱
		err = baseService.SendEmailVerificationCode(c, req.Email)
		// 排除错误 "error":"short response: \u0000\u0000\u0000\u001a\u0000\u0000\u0000"
		if err != nil && !strings.Contains(err.Error(), "short response") {
			global.Log.Error("send email failed", zap.Error(err))
			response.FailWithMessage("send email failed", c)
			return
		}
		response.OkWithMessage("send email successfully", c)
		return
	}
	response.FailWithMessage("Incorrect verificationCode code", c)
}

// QQLoginURL qq登录url
// @Tags      公共接口
// @Summary   qq登录url
// @Description qq登录url
// @accept    application/json
// @Produce   application/json
// @Success   200   {object}  response.Response{msg=string,data=string}  "发送成功"
// @Failure   400   {object}  response.Response  "请求参数错误"
// @Failure   500   {object}  response.Response  "内部服务器错误"
// @Router    /base/qqLoginURL [get]
func (b *BaseApi) QQLoginURL(c *gin.Context) {
	url := global.Config.QQ.QQLoginURL()
	response.OkWithData(url, c)
}
